package com.config;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
/**
 * 
 * 包      名 ：  com.config
 * 文 件 名 : MyHandShakeInterceptor
 * 描       述: 握手拦截器
 * 作        者： 超哥
 * 创建时间： 2016年8月14日 上午9:23:08
 * 版         本：1.0
 */
public class MyHandShakeInterceptor implements HandshakeInterceptor{

    /**
     * 握手之前，若返回false，则不建立链接
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
            Map<String, Object> attributes) throws Exception {
        // TODO Auto-generated method stub
    	//解决The extension [x-webkit-deflate-frame] is not supported问题  
        if(request.getHeaders().containsKey("Sec-WebSocket-Extensions")) {  
            request.getHeaders().set("Sec-WebSocket-Extensions", "permessage-deflate");  
        }  
        HttpServletRequest swq = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();    
        String roomId = swq.getParameter("roomId");
        String userId = swq.getParameter("user");
        if (!StringUtils.isEmpty(roomId)) {
        	 attributes.put("roomId",roomId );
	    }
        if (!StringUtils.isEmpty(userId)) {
        	  attributes.put("userId",userId );
	    }
       
      
        return true;
    }

    /**
     * 握手之后
     */
    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
            Exception exception) {
        // TODO Auto-generated method stub

    }

}